using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Security.Cryptography;
using PlugNT.Safe;
using PlugNT.Database.Common;
using PlugNT.Database.Common.Simple;
using PlugNT.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.DAL
{
    public class Field
    {

        private static string currTableName = WebConfig.TablePrefix + "field";



        public FieldInfo GetFieldInfo(DataRow dr)
        {
            FieldInfo model = new FieldInfo();

            model.id = (int)dr["id"];
            model.tabpostfix = (string)dr["tabpostfix"];
            model.title = (string)dr["title"];
            model.htmltype = (string)dr["htmltype"];
            model.htmldepth = (string)dr["htmldepth"];
            model.defaultvalue = (string)dr["defaultvalue"];
            model.bindclslist = (string)dr["bindclslist"];
            model.isbindsub = (bool)dr["isbindsub"];
            model.column = (string)dr["column"];
            model.coltype = (string)dr["coltype"];
            model.order = int.Parse(dr["order"].ToString());
            model.length = int.Parse(dr["length"].ToString());
            return model;
        }






        public FieldInfo GetFieldInfo(int id)
        {
            FieldInfo model = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 * from " + currTableName);
            strSql.Append(" where id=" + id.ToString());
            DataTable dt = DbHelper.TabQuery(strSql.ToString());
            if (dt.Rows.Count > 0)
            {
                model = GetFieldInfo(dt.Rows[0]);
            }
            return model;
        }










        public bool Add(FieldInfo model)
        {


            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName + "(");
            strSql.Append("[tabpostfix],[title],[htmltype],[htmldepth],[defaultvalue],[bindclslist],[isbindsub],[column],[coltype],[order],[length]");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.tabpostfix) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.title) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.htmltype) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.htmldepth) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.defaultvalue) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.bindclslist) + "',");
            strSql.Append(" " + (model.isbindsub?"1":"0") + ",");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.column) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.coltype) + "',");
            strSql.Append(" " + model.order.ToString() + ",");
            strSql.Append(" " + model.length.ToString());
            strSql.Append(")");


            bool flag = false;
            try
            {
                flag = DbHelper.ExecuteSql(strSql.ToString()) > 0;
            }
            catch(Exception ex) 
            {
                SysManager.LogErrorWriteln(strSql.ToString() + "\n" + ex.Message + "\n" + ex.StackTrace);
            }
            return flag;
        }






        public bool Update(FieldInfo model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("[title]='" + PlugNT.Safe.StringHelper.SqlFilter(model.title) + "',");
            strSql.Append("[htmltype]='" + PlugNT.Safe.StringHelper.SqlFilter(model.htmltype) + "',");
            strSql.Append("[htmldepth]='" + PlugNT.Safe.StringHelper.SqlFilter(model.htmldepth) + "',");
            strSql.Append("[defaultvalue]='" + PlugNT.Safe.StringHelper.SqlFilter(model.defaultvalue) + "',");
            strSql.Append("[bindclslist]='" + PlugNT.Safe.StringHelper.SqlFilter(model.bindclslist) + "',");
            strSql.Append("[isbindsub]=" + (model.isbindsub ? "1" : "0") + ",");
            strSql.Append("[column]='" + PlugNT.Safe.StringHelper.SqlFilter(model.column) + "',");
            strSql.Append("[coltype]='" + PlugNT.Safe.StringHelper.SqlFilter(model.coltype) + "',");
            strSql.Append("[order]=" + model.order.ToString() + ",");
            strSql.Append("[length]=" + model.length.ToString());
            strSql.Append(" where [id]=" + model.id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }









        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + currTableName);
            strSql.Append(" where id=" + id.ToString());
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }











        public DataTable GetList(string andWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from  [" + currTableName + "] where 1=1 " + andWhere);
            return DbHelper.TabQuery(strSql.ToString());
        }







        public DataTable GetBindList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.*,b.description from  [" + currTableName +
                "] as a left join " + Tables.currTableName +
                " as b on a.tabpostfix=b.postfix ");

            return DbHelper.TabQuery(strSql.ToString());
        }






        public DataTable GetBindList(string tablePostfix)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.*,b.description from  [" + currTableName +
                "] as a left join " +Tables.currTableName +
                " as b on a.tabpostfix=b.postfix " + " where a.tabpostfix='" + tablePostfix + "'");



            return DbHelper.TabQuery(strSql.ToString());
        }








        public int GetCount(string andWhere)
        {
            string sql = "select count(1) from [" + currTableName + "] where 1=1 " + andWhere;
            return (int)DbHelper.GetSingle(sql);
        }







        public bool Exists(string columnname, string  tablePostfix)
        {
            if (columnname != "")
            {
                string sql = "select count(1) from sysobjects where id in (select id from syscolumns where name='"
                    + PlugNT.Safe.StringHelper.SqlFilter(columnname) + "') and name= '" 
                    + Article.currTableName + tablePostfix + "'";
                return ((int)DbHelper.GetSingle(sql))>0;
            }
            else
            {
                return false;
            }
        }


        public string Help()
        {
            return @"mailto:354990393@qq.com";
        }


    }
}
